using Holoville.HOTween;
using Holoville.HOTween.Core;
using UnityEngine;

public class PoliceManager : MonoBehaviour
{
	public static PoliceManager Instance;

	public WarningLevel currentWarningLevel;

	private int chasingTime = 30;

	[HideInInspector]
	public UIPanel wLabel;

	private bool _IsChasingPaused;

	private void Awake()
	{
		Instance = this;
	}

	private void Blink()
	{
		if (wLabel == null)
		{
			wLabel = GameTopWindow.instance.policeWarning;
		}
		if (wLabel != null && wLabel.alpha == 0f)
		{
			ToHiAlpha();
			GameTopWindow.instance.HideAggressiveLabel();
		}
	}

	private void StopBlink()
	{
		HOTween.Kill(wLabel);
		if (wLabel != null)
		{
			wLabel.alpha = 0f;
		}
		GameTopWindow.instance.ToggleAggressiveLabel();
	}

	private void ToLowAlpha()
	{
		HOTween.Kill(wLabel);
		HOTween.To(wLabel, 0.5f, new TweenParms().Prop("alpha", 0.1).Ease(EaseType.Linear).OnComplete((TweenDelegate.TweenCallback)delegate
		{
			ToHiAlpha();
		}));
	}

	private void ToHiAlpha()
	{
		HOTween.Kill(wLabel);
		HOTween.To(wLabel, 0.5f, new TweenParms().Prop("alpha", 1).Ease(EaseType.Linear).OnComplete((TweenDelegate.TweenCallback)delegate
		{
			ToLowAlpha();
		}));
	}

	private void Start()
	{
		if (!settings.offlineMode)
		{
			base.enabled = false;
			return;
		}
		wLabel = GameTopWindow.instance.policeWarning;
		if (wLabel == null)
		{
			wLabel = GameObject.FindGameObjectWithTag("GUI_PoliceWarning").GetComponent<UIPanel>();
		}
		wLabel.alpha = 0f;
	}

	public void StopChasing()
	{
		if (!GameController.instance.playerScript.isDead && GetCurrentWarningLevel() && settings.offlineMode)
		{
			AllUnitaryAchievements.instance.escapeFromPolice_30.Increment();
			GameplayRewardManager.instance.AddToRewardQueue(AllKillEvents.instance.EscapeFromPolice, true);
		}
		foreach (EnemyBehavior mobs in MobSpawnController.instance.MobsList)
		{
			if (mobs != null && mobs.playerTarget != null)
			{
				if (mobs.isPolice)
				{
					mobs.isAggressive = false;
					mobs.SwitchEnemyState(EnemyState.Passive);
					mobs.toggleGun(false);
					mobs.ShowAgroZone(false);
				}
				if (mobs.isPussy)
				{
					mobs.isShocked = false;
					mobs.SwitchEnemyState(EnemyState.Passive);
				}
				if (mobs.behaviorModel == BehaviorModel.Passive)
				{
					mobs.isAggressive = false;
					mobs.SwitchEnemyState(EnemyState.Passive);
					mobs.toggleGun(false);
					mobs.ShowAgroZone(false);
				}
			}
		}
		_IsChasingPaused = false;
		SwitchWarningLevel(WarningLevel.Normal);
		StopBlink();
		CancelInvoke("Blink");
	}

	public void UpdateChasingTimer()
	{
		CancelInvoke("StopChasing");
		Invoke("StopChasing", chasingTime + 10);
		SwitchWarningLevel(WarningLevel.Chasing);
	}

	public void PauseChasing()
	{
		_IsChasingPaused = true;
		foreach (EnemyBehavior mobs in MobSpawnController.instance.MobsList)
		{
			if (mobs != null && mobs.playerTarget != null && mobs.isPolice)
			{
				mobs.isAggressive = false;
				mobs.SwitchEnemyState(EnemyState.Passive);
				mobs.toggleGun(false);
			}
		}
	}

	public void StartChasing(bool isReturnToChasing = false)
	{
		if (!isReturnToChasing)
		{
			Invoke("Blink", 0.1f);
			CancelInvoke("StopChasing");
			Invoke("StopChasing", chasingTime);
		}
		if (GameController.instance.playerScript.CanBeAttacked())
		{
			foreach (EnemyBehavior mobs in MobSpawnController.instance.MobsList)
			{
				if (mobs.isPolice)
				{
					mobs.GetDamage(0, 0);
					mobs.ShowAgroZone(true);
				}
				if (mobs.behaviorModel == BehaviorModel.Passive)
				{
					mobs.GetDamage(0, 0);
					mobs.ShowAgroZone(true);
				}
				if (mobs.isPussy)
				{
					mobs.GetDamage(0, 0);
				}
			}
			return;
		}
		_IsChasingPaused = true;
	}

	public void SwitchWarningLevel(WarningLevel w)
	{
		switch (w)
		{
		case WarningLevel.Normal:
			currentWarningLevel = w;
			break;
		case WarningLevel.Chasing:
			currentWarningLevel = w;
			StartChasing();
			break;
		}
	}

	public bool GetCurrentWarningLevel()
	{
		if (currentWarningLevel == WarningLevel.Chasing)
		{
			return true;
		}
		return false;
	}

	private void Update()
	{
		if (GameController.instance.playerScript != null && GameController.instance.playerScript.isDead)
		{
			StopChasing();
		}
		WarningLevel warningLevel = currentWarningLevel;
		if (warningLevel != 0 && warningLevel == WarningLevel.Chasing)
		{
			if (_IsChasingPaused && GameController.instance.playerScript.CanBeAttacked())
			{
				_IsChasingPaused = false;
				StartChasing(true);
			}
			else if (!_IsChasingPaused && !GameController.instance.playerScript.CanBeAttacked())
			{
				PauseChasing();
			}
		}
	}
}
